//
//  SEAuthTextFildView.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/12.
//

import UIKit

enum SEAuthTextFieldViewType: Int {
    case account
    case oldPassword
    case password
    case confirmPsd
    case code

    var logoImage: UIImage? {
        switch self {
        case .account: return Asset.authAccountIcon.image
        default:       return Asset.authPasswordIcon.image
        }
    }

    var placeHoder: String {
        switch self {
        case .account:
            return "请输入手机号"
        case .oldPassword:
            return "请输入旧密码"
        case .password:
            return "请输入新密码"
        case .confirmPsd:
            return "请再次输入新密码"
        case .code:
            return "请输入验证码"
        }
    }
}

protocol SEAuthTextFildViewDelegate: NSObjectProtocol {
    func authTextFildViewCodeBtnAction()
}

class SEAuthTextFildView: UIView {

    @IBOutlet weak var logoImgView: UIImageView!{
        didSet {
            logoImgView.isHidden = isHiddenLogoImgView
            logoImgView.image = viewType.logoImage
        }
    }
    @IBOutlet weak var textField: UITextField! {
        didSet {
            textField.placeholder = viewType.placeHoder
        }
    }
    @IBOutlet weak var codeBtn: UIButton! {
        didSet {
            codeBtn.layer.cornerRadius = codeBtn.size.height / 2.0
            codeBtn.layer.masksToBounds = true
        }
    }
    @IBOutlet weak var eyeBtn: UIButton!
    @IBOutlet weak var textFieldLeading: NSLayoutConstraint!
    @IBOutlet weak var textFieldTrailing: NSLayoutConstraint!

    
    @IBAction func codeBtnAction(_ sender: UIButton) {
        delegate?.authTextFildViewCodeBtnAction()
    }

    @IBAction func eyeBtnAction(_ sender: UIButton) {
        eyeBtn.isSelected.toggle()
        textField.isSecureTextEntry = !eyeBtn.isSelected
    }

    /// 秒时间戳
    private var timeStamp: Int = 60

    private var timer: SETimer?

    weak var delegate: SEAuthTextFildViewDelegate?

    private var authTextFieldviewType: SEAuthTextFieldViewType = .account {
        didSet {
            logoImgView.image = authTextFieldviewType.logoImage
            textField.placeholder = authTextFieldviewType.placeHoder
            textField.isSecureTextEntry = false
            switch authTextFieldviewType {
            case .account:
                eyeBtn.isHidden = true
                codeBtn.isHidden = true
                textFieldTrailing.constant = 24
            case .oldPassword, .password, .confirmPsd:
                eyeBtn.isHidden = false
                codeBtn.isHidden = true
                textFieldTrailing.constant = 54
                textField.isSecureTextEntry = true
            case .code:
                eyeBtn.isHidden = true
                codeBtn.isHidden = false
                textFieldTrailing.constant = 122
            }
        }
    }

    private var isHiddenLogoImgView: Bool = false {
        didSet {
            logoImgView.isHidden = isHiddenLogoImgView
            if isHiddenLogoImgView {
                textFieldLeading.constant = 24
            } else {
                textFieldLeading.constant = 54
            }
        }
    }

    var viewType: SEAuthTextFieldViewType {
        get {
            return self.authTextFieldviewType
        }
        set {
            authTextFieldviewType = newValue
        }
    }

    var isHiddenLogo: Bool {
        get {
            return isHiddenLogoImgView
        }
        set {
            isHiddenLogoImgView = newValue
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setUI()
    }

    private func setUI() {
        loadNib()
        viewType = .account
        layer.cornerRadius = textField.frame.size.height / 2.0
        layer.borderWidth = 1.0
        layer.borderColor = UIColor.init(hex: "E7E7E7").cgColor
        layer.masksToBounds = true
    }

    private func loadNib() {
        let view = UINib.init(nibName: String(describing: type(of: self)), bundle: Bundle(for: type(of: self))).instantiate(withOwner: self, options: nil).last as! UIView
        view.frame = bounds
        addSubview(view)
    }

    func fireTimer(with timeInterval: Int = 60) {
        codeBtn.isEnabled = false
        timeStamp = timeInterval
        timer = SETimer.scheduledTimer(with: 1, repeats: true, block: { [weak self] _ in
            self?.countDownTime()
        })
    }

    private func countDownTime() {
        if timeStamp == 0 {
            codeBtn.isEnabled = true
            codeBtn.setTitle("获取验证码", for: .normal)
            timer?.invalidate()
        } else {
            let text = "再次获取\(timeStamp)S"
            codeBtn.titleLabel?.text = text
            codeBtn.setTitle(text, for: .normal)
            timeStamp -= 1
        }
    }

}
